#include <iostream>
#include "ULUI_MainMenu.h"
#include "ULUI_HistoryLookup.h"
#include "ULUI_StatisticLookup.h"
#include "ULUI_Prediction.h"
#include "ULUI_UpdateHistory.h"
#include "ULUI_Help.h"
#include "UL_Global.h"

ULUI_MainMenu ULUI_MainMenu::mainMenu;

#define MAIN_MENU_START_X	100
#define MAIN_MENU_START_Y	100

enum MainMenuIDs{
	HISTORY,
	STATISTIC,
	PREDICTION,
	UPDATE_HISTORY,
	HELP,
	EXIT,
};

static char *menuTitles[] = {
	"历史数据查询",
	"统计查看",
	"下期预测",
	"更新历史数据",
	"规则帮助",
	"退出"
};

void ULUI_MainMenu::SetFont(VSDL_Font *font)
{
	mainMenu.font = font;
}

void ULUI_MainMenu::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);
	//Setup Menus
	SetFont(UL_Global::UnionLottoFont());
	menu.SetFont(font);

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}
		
	menus.SetFirstMenuXY(VSDL_Point(MAIN_MENU_START_X, MAIN_MENU_START_Y));

	selectedMenu = -1;
}

void ULUI_MainMenu::CleanUp()
{
}

void ULUI_MainMenu::Pause()
{
}

void ULUI_MainMenu::Resume()
{
	selectedMenu = -1;
}

void ULUI_MainMenu::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case HISTORY:
			game->PushState(ULUI_HistoryLookup::Instance());
			break;
		case STATISTIC:
			game->PushState(ULUI_StatisticLookup::Instance());
			break;
		case PREDICTION:
			game->PushState(ULUI_Prediction::Instance());
			break;
		case UPDATE_HISTORY:
			game->PushState(ULUI_UpdateHistory::Instance());
			break;
		case HELP:
			game->PushState(ULUI_Help::Instance());
			break;
		case EXIT:
			game->Quit();
			break;
	}

	selectedMenu = -1;

}	

void ULUI_MainMenu::Draw(VSDL_GameEngine *game)
{
	//game->FillScreen(VSDL_RGB(255, 255, 255));
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(game->GetSurface());

}

ULUI_MainMenu *ULUI_MainMenu::Instance()
{
	return &mainMenu;
}

void __HANDLE_EVENT ULUI_MainMenu::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->Quit();	
	}
}

void __HANDLE_EVENT ULUI_MainMenu::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_MainMenu::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (int i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;	
		}		
	}
}

void __HANDLE_EVENT ULUI_MainMenu::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}

